package com.dl.exam.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dl.exam.entity.Page;
import com.dl.exam.entity.User;
import com.dl.exam.entity.question.Question;
import com.dl.exam.mapper.UserMapper;
import com.dl.exam.service.AdminService;
import com.dl.exam.util.PageUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author DL
 * @create 2022-01-19 14:40
 */
@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean modifyState(String userId, int state) {
        return userMapper.updateState(userId, state) != 0;
    }

    @Override
    public boolean modifyUserRole(String userId, JSONArray roleIdList) {
        System.out.println("roleIdList = " + roleIdList);

        int delete = userMapper.deleteUserRoleById(userId);
        int insert = userMapper.insertUserRole(userId, roleIdList);
        return delete != 0 && insert != 0;
    }

    @Override
    public boolean modifyUserAllInfo(JSONObject user) {
        System.out.println("user = " + user);
        return userMapper.updateUserAllInfo(user) != 0;
    }

    @Override
    public User getUserAllInfo(String userId) {
        return userMapper.queryUserAllInfo(userId);
    }

    @Override
    public Page<User> getAllUser(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<User> list = userMapper.queryAllUser();
        return (Page<User>) PageUtils.getPage(list);
    }

    @Override
    public Page<User> getUserByCondition(JSONObject condition) {
        PageHelper.startPage(condition.getInteger("currentPage"), 10);
        List<User> list = userMapper.queryUserByCondition(condition);
        return (Page<User>) PageUtils.getPage(list);
    }

    @Override
    public Page<User> getUserByRoleId(List<Integer> roleIdList, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<User> list = userMapper.queryUserByRoleId(roleIdList);
        return (Page<User>) PageUtils.getPage(list);
    }
}
